<template>
  <div class="site-map">
    <div class="site-top">
      <Icon class="close-fill"
            @click="closeSite"
            icon="close-fill" />

      <span>路口名称</span>
    </div>
    <D-map @inspection="inspectionFn" />
    <div class="site-button"
         :class="{'start-site': siteFlag}"
         @click="startSite">
      <span v-text="title"></span>
    </div>
  </div>
</template>

<script lang="ts">
import Icon from '/@/components/Icon/Icon.vue'
import DMap from '/@/components/map/Map.vue'
import { defineComponent, reactive, toRefs } from 'vue'
import { useRouter } from 'vue-router'
import { Dialog } from 'vant'

export default defineComponent({
  components: {
    DMap,
    Icon,
  },
  setup() {
    const state = reactive({
      map: null,
      siteFlag: false,
      title: '开始',
    })

    const router = useRouter()

    const startSite = () => {
      state.siteFlag = !state.siteFlag
      state.title = state.siteFlag ? '结束' : '开始'
    }

    const closeSite = () => {
      if (state.siteFlag) {
        Dialog.confirm({
          message: '正在巡检中，退出后结束巡检，是否退出？',
          confirmButtonColor: 'rgba(0,125,277,1)',
        })
          .then(() => {
            state.siteFlag = false
            state.title = '开始'
            router.back()
          })
          .catch(() => {
            // on cancel
          })
        return
      }
      router.back()
    }

    const inspectionFn = (item: any) => {
      console.log(item)
    }

    return {
      closeSite,
      startSite,
      inspectionFn,
      ...toRefs(state),
    }
  },
})
</script>

<style lang="less" scoped>
.site-map {
  position: relative;
  width: 100%;
  height: 100vh;
  .site-top {
    position: absolute;
    top: 2%;
    left: 2%;
    z-index: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 96%;
    height: 50px;
    background: white;
    border-radius: 4px;
    box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3);
    .close-fill {
      position: absolute;
      top: 30%;
      left: 2%;
      font-size: 24px;
    }
    > span {
      font-size: 16px;
    }
  }
  .site-button {
    position: absolute;
    bottom: 10%;
    left: 40%;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 75px;
    height: 75px;
    z-index: 1;
    padding-bottom: 5px;
    border-radius: 50%;
    background-color: rgb(0, 125, 227);
    > span {
      font-size: 14px;
      color: white;
    }
    &::after {
      position: absolute;
      top: -5px;
      left: -5px;
      z-index: -1;
      content: '';
      width: 85px;
      height: 85px;
      border-radius: 50%;
      background-color: rgb(0, 125, 227);
      opacity: 0.29;
    }

    &.start-site {
      background-color: rgb(255, 153, 0);
      &::after {
        position: absolute;
        top: -5px;
        left: -5px;
        z-index: -1;
        content: '';
        width: 85px;
        height: 85px;
        border-radius: 50%;
        background-color: rgb(255, 153, 0);
        opacity: 0.29;
      }
    }
  }
}
</style>